package edu.scau.mis.sys.service.impl;

import edu.scau.mis.sys.entity.SysUserRole;
import edu.scau.mis.sys.mapper.SysUserRoleMapper;
import edu.scau.mis.sys.service.SysUserRoleService;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;

/**
 * (SysUserRole)表服务实现类
 *
 * @author makejava
 * @since 2022-03-02 23:36:10
 */
@Service("sysUserRoleService")
public class SysUserRoleServiceImpl implements SysUserRoleService {
    @Resource
    private SysUserRoleMapper sysUserRoleMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public SysUserRole queryById(Long id) {
        return this.sysUserRoleMapper.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param sysUserRole 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<SysUserRole> queryByPage(SysUserRole sysUserRole, PageRequest pageRequest) {
        long total = this.sysUserRoleMapper.count(sysUserRole);
        return new PageImpl<>(this.sysUserRoleMapper.queryAllByLimit(sysUserRole, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param sysUserRole 实例对象
     * @return 实例对象
     */
    @Override
    public SysUserRole insert(SysUserRole sysUserRole) {
        this.sysUserRoleMapper.insert(sysUserRole);
        return sysUserRole;
    }

    /**
     * 修改数据
     *
     * @param sysUserRole 实例对象
     * @return 实例对象
     */
    @Override
    public SysUserRole update(SysUserRole sysUserRole) {
        this.sysUserRoleMapper.update(sysUserRole);
        return this.queryById(sysUserRole.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.sysUserRoleMapper.deleteById(id) > 0;
    }

    /**
     * 插入用户角色
     *
     * @param adminId
     * @param roleId
     * @return
     */
    @Override
    public int insertRoleUser(long adminId, long roleId){return this.sysUserRoleMapper.insertRoleUser(adminId, roleId);}

    /**
     * 通过用户id删除数据
     *
     * @param userId 主键
     * @return 影响行数
     */
    @Override
    public int deleteByUserId(Long userId){return this.sysUserRoleMapper.deleteByUserId(userId);}
}
